<?php
include 'template/header.html';
require_once 'lib/function.php';

    $action = isset($_GET['action'])?$_GET['action']:'list';
    switch ($action) {
        case 'list':
            $sql = "select * from customerstat";
            $res = getAllData($sql);
            include 'template/customer_stat_list.html';

            break;
        case 'add':
            include 'template/customer_stat_add.html';
            break;
        case 'add_check':
            $stat = isset($_POST['stat'])?$_POST['stat']:'';
            $info = isset($_POST['info'])?$_POST['info']:'';
            $sql = "select * from customerstat where cstat_stat = '$stat'";
            $res = getRowNum($sql);
            if($res>0){
                echo "<script>alert('已有的状态类型！');location.href='cstat.php?action=add'</script>";
            }
            $sql = "insert into customerstat values('','$stat','$info')";
            $res = dclData($sql);
            if($res){
                echo "<script>alert('添加成功!');location.href='cstat.php'</script>";
            }
            break;
        case 'delete':
            $cstat_id = $_GET['cstat_id'];
            $sql = "delete from customerstat where cstat_id='$cstat_id'";
            $res = dclData($sql);
            if($res){
                echo "<script>alert('删除成功!');location.href='cstat.php'</script>";
            }
            break;
        case 'update':
            $cstat_id = $_GET['cstat_id'];
            $sql = "select * from customerstat where cstat_id='$cstat_id'";
            $res = getAllData($sql);
            $res = $res[0];
            include 'template/customer_stat_add.html';
            break;
        case 'update_check':
            $cstat_id = $_GET['cstat_id'];
            $stat = $_POST['stat'];
            $info = $_POST['info'];
            $sql = "update customerstat set cstat_stat='$stat',cstat_info='$info' where cstat_id='$cstat_id'";
            $res = dclData($sql);
            if($res){
                echo "<script>alert('更新成功!');location.href='cstat.php'</script>";
            }
            break;
    }
    include 'template/foot.html';